import 'package:flutter/material.dart';

class PathBean {
  PathBean();
  List<Offset> line = [];
  Color color = Colors.blue;
  bool contain(Offset localPosition) {
    Path path = Path();
    for (var i = 0; i < line.length; i++) {
      var l = line[i];
      if (i == 0) {
        path.moveTo(l.dx, l.dy);
      } else {
        path.lineTo(l.dx, l.dy);
      }
    }
    path.close();
    return path.contains(localPosition);
  }

  Map<String, dynamic> toJson() {
    return {
      'line': line.map((e) => {'dx': e.dx, 'dy': e.dy}).toList(),
      'color': color.value,
    };
  }

  factory PathBean.fromJson(Map<String, dynamic> json) {
    return PathBean()
      ..line = (json['line'] as List)
          .map(
            (e) => Offset(
              (e['dx'] as num).toDouble(),
              (e['dy'] as num).toDouble(),
            ),
          )
          .toList()
      ..color = Color(json['color'] as int);
  }
}
